package com.shoutao.website.controller.datacontroller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shoutao.website.contact.*;
import com.shoutao.website.entity.Productcomment;
import com.shoutao.website.entity.Productinfo;
import com.shoutao.website.entity.Productparameterinfo;
import com.shoutao.website.entity.Productpicinfo;
import com.shoutao.website.mapper.ProductcommentMapper;
import com.shoutao.website.mapper.ProductinfoMapper;
import com.shoutao.website.mapper.ProductparameterinfoMapper;
import com.shoutao.website.mapper.ProductpicinfoMapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description:
 * @Param:
 * @return:
 * @Author: cqin
 * @Date: 2019/11/5
 */
@RestController
@RequestMapping(value = "/api/product")
public class ProductController {


    @Autowired
    private ProductinfoMapper productinfoMapper;
    @Autowired
    private ProductpicinfoMapper productpicinfoMapper;
    @Autowired
    private ProductcommentMapper productcommentMapper;
    @Autowired
    private ProductparameterinfoMapper productparameterinfoMapper;


    @ApiOperation(value = "产品显示/查询接口", notes = "产品显示/查询接口")
    @RequestMapping(value = "/searchProduct.do", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "请求", dataType = "SearchProductRequest", required = true)
    })
    public SearchProductResponse searchProduct(HttpServletRequest httpServletRequest, SearchProductRequest request) throws SQLException {

        SearchProductResponse response = new SearchProductResponse();
        List<Productinfo> resultList = new ArrayList<>();
        List<Productinfo> productinfoList = productinfoMapper.selectList(null);
        if (StringUtils.isNotBlank(request.getTags())) {
            for (Productinfo productinfo : productinfoList) {
                if (productinfo.getProducttags().contains(request.getTags())) {
                    resultList.add(productinfo);
                }
            }
        }
        if (StringUtils.isNotBlank(request.getRelativeIds())) {
            productinfoList.forEach(x -> {
                if (Arrays.stream(request.getRelativeIds().split(";")).anyMatch(p -> p.equals(x.getId().toString())))
                    resultList.add(x);
            });
        }
        response.setProductinfoList(resultList);
        return response;
    }


    @ApiOperation(value = "产品提交接口", notes = "产品提交接口")
    @RequestMapping(value = "/saveProduct.do", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "请求", dataType = "EditProductRequest", required = true)
    })
    public EditProductResponse saveProduct(HttpServletRequest httpServletRequest, EditProductRequest request) throws SQLException {


        EditProductResponse response = new EditProductResponse();

        HttpSession httpSession = httpServletRequest.getSession();
        if (httpSession == null || httpSession.getAttribute("user") == null
                || !"admin".equals((String) httpSession.getAttribute("user"))) {
            response.setResult(false);
            response.setResultMessage("没有足够的权限");
            return response;
        }


        if (request.getProductId() != 0) {
            Productinfo productinfo = productinfoMapper.selectById(request.getProductId());
            if (productinfo == null) {
                response.setResult(false);
                return response;
            }

            mapProductinfo(productinfo, request);
            productinfoMapper.updateById(productinfo);

            response.setResult(true);
            response.setResultMessage("更新成功");
        } else {
            Productinfo productinfo = new Productinfo();
            mapProductinfo(productinfo, request);
            productinfoMapper.insert(productinfo);

            // 需要往productpic里插入数据
            Productpicinfo productpicinfo = new Productpicinfo();
            productpicinfo.setProductid(productinfo.getId());
            productpicinfo.setPicurl(request.getDetailPicurl());
            productpicinfoMapper.insert(productpicinfo);

            response.setResult(true);
            response.setResultMessage("新增成功");
        }
        return response;

    }

    private void mapProductinfo(Productinfo productinfo, EditProductRequest request) {
        productinfo.setProductname(request.getProductname());
        productinfo.setProductdesc(request.getProductdesc());
        productinfo.setProducttags(request.getProducttags());
        productinfo.setProducttype(request.getProductType());
        productinfo.setThumbnailspicurl(request.getThumbnailsPicurl());
        productinfo.setModelno(request.getModelno());
        productinfo.setStandard(request.getStandard());
        productinfo.setGuage(request.getGuage());
        productinfo.setColor(request.getColor());
        productinfo.setAvailablesize(request.getAvailableSize());
        productinfo.setProductstar(request.getProductstar());
        productinfo.setProductattribute(request.getProductattribute());
        productinfo.setRelativeid(request.getRelativeId());
    }


    @ApiOperation(value = "产品详情接口", notes = "产品详情接口")
    @RequestMapping(value = "/showProductDetail.do", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "请求", dataType = "SearchProductRequest", required = true)
    })
    public ShowProductDetailResponse showProductDetail(HttpServletRequest httpServletRequest, ShowProductDetailRequest request) throws SQLException {

        ShowProductDetailResponse response = new ShowProductDetailResponse();

        // 基础信息
        Productinfo productinfo = productinfoMapper.selectById(request.getProdcutId());
        response.setProductinfo(productinfo);

        // 图片
        Productpicinfo productpicinfoSample = new Productpicinfo();
        productpicinfoSample.setProductid(request.getProdcutId());
        List<Productpicinfo> productpicinfoList = productpicinfoMapper.selectList(new QueryWrapper<>(productpicinfoSample));
        response.setProductpicinfoList(productpicinfoList);


        // 评论
        Productcomment productcommentSample = new Productcomment();
        productcommentSample.setProductid(request.getProdcutId());
        List<Productcomment> productcommentList = productcommentMapper.selectList(new QueryWrapper<>(productcommentSample));
        response.setProductcommentList(productcommentList);

        // 参数
        Productparameterinfo productparameterinfoSample = new Productparameterinfo();
        productparameterinfoSample.setProductid(request.getProdcutId());
        List<Productparameterinfo> productparameterinfoList = productparameterinfoMapper.selectList(new QueryWrapper<>(productparameterinfoSample));
        response.setProductparameterinfoList(productparameterinfoList);

        return response;
    }


}
